package com.yuneec.fly.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.Stack;

import com.yuneec.fly.DroneApplication;
import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.presenters.DronePresenter;

public class AppManager {

    private Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        Log.d("zzd","addActivity:"+activityStack.size());
        if (activityStack.size() == 0) {
            DronePresenter.getInstance().initService();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束指定的Activity
     */
    public boolean hasStartedActivity(Class<?> cls) {
        if (activityStack == null) {
            return false;
        }
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
        if (activityStack.size() == 0) {
            AppExit();
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        try {
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) {
                    finishActivity(activity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (activityStack.size() == 0) {
            AppExit();
        }
    }

    /**
     * 结束所有Activity
     */
    private void finishAllActivity() {
        if (activityStack.isEmpty()) {
            return;
        }
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 将最后一个activity以外的所有activity结束
     */
    public void finishAllActivityWithoutLast() {
        if (activityStack.isEmpty()) {
            return;
        }
        Activity activity = activityStack.pop();
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
        activityStack.add(activity);
    }

    /**
     * 退出应用程序
     */
    public void AppExit() {
        try {
            GlobalParams.exit();
            Context context = DroneApplication.getApplication();
            DronePresenter.getInstance().exit();
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
//			android.os.Process.killProcess(android.os.Process.myPid());
//			System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isEmpty() {
        return activityStack == null ? true : activityStack.isEmpty();
    }
}